﻿var _ajaxNumericPagger = {
    generatePagger: function ( formId, pageCount, num_display_entries, prevLabel, nextLabel ) {
        var pageIndex = $( '#' + formId ).find( ':hidden[name=pageIndex]:first' ).val();
        pageIndex = !isNaN( pageIndex ) ? parseInt( pageIndex ) : 0;
        var $numlist = $( '#' + formId ).find( '.pagger-numeric-list' );
        if ( $numlist == null ) return;
        $numlist.empty();

        //previous button
        if ( pageIndex > 0 ) {
            var $prevBtn = $( '<input type="button" value="' + prevLabel + '"/>' )
            .click( function () {
                $( '#' + formId ).find( ':hidden[name=pageIndex]:first' ).val( pageIndex - 1 );
                $( '#' + formId ).submit();
            } );
            $numlist.append( $prevBtn );
        }
        //get numeric range
        var numRange = this._getRange( pageIndex, pageCount, num_display_entries );
        //first numeric button
        if ( numRange.length > 0 && numRange[0] > 0 ) {
            var $fr1Btn = $( '<input type="button" value="1"/>' )
            .click( function () {
                $( '#' + formId ).find( ':hidden[name=pageIndex]:first' ).val( '0' );
                $( '#' + formId ).submit();
            } );
            $numlist.append( $fr1Btn );
            $numlist.append( '<span>...</span>' );
        }
        //numeric buttons
        for ( var i = 0; i < numRange.length; i++ ) {
            var $numBtn;
            if ( numRange[i] != pageIndex ) {
                $numBtn = $( '<input type="button" value="' + ( numRange[i] + 1 ).toString() + '"/>' )
                .click( function () {
                    var num = $( this ).val();
                    num = !isNaN( num ) ? parseInt( num ) - 1 : 0;
                    $( '#' + formId ).find( ':hidden[name=pageIndex]:first' ).val( num );
                    $( '#' + formId ).submit();
                } );
            } else {
                $numBtn = $( '<span>' + ( numRange[i] + 1 ).toString() + '</span>' );
            }
            $numlist.append( $numBtn );
        }
        //last numeric button
        if ( numRange[numRange.length - 1] < pageCount - 1 ) {
            $numlist.append( '<span>...</span>' );
            var $lastBtn = $( '<input type="button" value="' + pageCount + '"/>' )
            .click( function () {
                $( '#' + formId ).find( ':hidden[name=pageIndex]:first' ).val( pageCount - 1 );
                $( '#' + formId ).submit();
            } );
            $numlist.append( $lastBtn );
        }
        //next button
        if ( pageIndex < pageCount - 1 ) {
            var $nextBtn = $( '<input type="button" value="' + nextLabel + '"/>' )
            .click( function () {
                $( '#' + formId ).find( ':hidden[name=pageIndex]:first' ).val( pageIndex + 1 );
                $( '#' + formId ).submit();
            } );
            $numlist.append( $nextBtn );
        }

    },
    _getRange: function ( pageIndex, pageCount, num_display_entries ) {
        var ne_half = Math.ceil( num_display_entries / 2 );
        var upper_limit = pageCount - ne_half;
        var start = pageIndex > ne_half ? ( Math.max( Math.min( pageIndex - ne_half, upper_limit ), 0 ) ) : 0;
        var end = pageIndex > ne_half ? Math.min( pageIndex + ne_half, pageCount ) : Math.min( num_display_entries, pageCount );
        var range = [];
        for ( var i = start; i < end; i++ ) {
            range.push( i );
        }
        return range;
    }
};